/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.event;

import com.google.common.eventbus.Subscribe;
import com.intelligent.ispc.client.core.view.analysis.grr.AnalysisPanel;
import com.intelligent.ispc.client.core.view.analysis.grr.GrrMainPanel;
import com.intelligent.ispc.client.core.view.analysis.report.YieldPanel;
import com.intelligent.ispc.client.core.view.analysis.spc.SpcMainPanel;
import com.intelligent.ispc.client.core.view.global.MainStateBar;
import com.intelligent.ispc.client.utils.ApplicationBeanUtils;
import com.intelligent.ispc.constant.ProcessConstant;
import com.intelligent.ispc.core.dto.*;

import java.util.List;

/**
 * Created by Peter on 2016/9/7.
 * Updated by Lucien.Chen on 2016/9/13
 */
public class ExecutorTaskFinishListener {

    /**
     * Event subscribe method for finishing spc task.
     *
     * @param event ExecutorTaskFinishEvent instance
     */
    @Subscribe
    public void spcDecodeFileFinish(ExecutorTaskFinishEvent event) {
        try {
            if (event.getKey().equals(ProcessConstant.SPC_DECODE_FILE)) {
                if (event.getResult() != null) {
                    FieldDataSourceDto dto = (FieldDataSourceDto) event.getResult();
                    ApplicationBeanUtils.getBean(SpcMainPanel.class).getAnalysisItemPanel().onReceivedTestItems(dto.getTestItemNames());
                    ApplicationBeanUtils.getBean(SpcMainPanel.class).getAnalysisSearchPanel().setBaseFields(dto.getSearchFields());
                    ApplicationBeanUtils.getBean(SpcMainPanel.class).analysisSuccessful();
                }
            } else if (event.getKey().equals(ProcessConstant.SPC_EXECUTE_TASK)) {
                List<SpcResultDto> resultDtos = (List<SpcResultDto>) event.getResult();
                if (resultDtos != null && !resultDtos.isEmpty()) {
                    ApplicationBeanUtils.getBean(SpcMainPanel.class).searchSuccessful(resultDtos.get(0));
                } else {
                    ApplicationBeanUtils.getBean(SpcMainPanel.class).searchSuccessful(null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Event subscribe method for finishing yield task.
     *
     * @param event ExecutorTaskFinishEvent instance
     */
    @Subscribe
    public void yieldChange(ExecutorTaskFinishEvent event) {
        try {
            if (event.getKey().equals(ProcessConstant.YIELD_DECODE_FILE)) {
                if (event.getResult() != null && !ApplicationBeanUtils.getBean(MainStateBar.class).getProcessPanel().isProcessTaskEmpty()) {
                    FieldDataSourceDto dto = (FieldDataSourceDto) event.getResult();
                    ApplicationBeanUtils.getBean(YieldPanel.class).setGroupKeys(dto.getSearchAndAttributeFields());
                    ApplicationBeanUtils.getBean(YieldPanel.class).initGroup();
                    ApplicationBeanUtils.getBean(YieldPanel.class).getAnalysisItemPanel().onReceivedTestItems(dto.getTestItemNames());
                    ApplicationBeanUtils.getBean(YieldPanel.class).getAnalysisSearchPanel().setBaseFields(dto.getSearchFields());
                }
            } else if (event.getKey().equals(ProcessConstant.YIELD_EXECUTE_TASK)) {
                List<YieldDto> yieldDtos = (List<YieldDto>) event.getResult();
                ApplicationBeanUtils.getBean(YieldPanel.class).setYieldData(yieldDtos);
            } else if (event.getKey().equals(ProcessConstant.YIELD_HISTORY_TASK)) {
                YieldHistoryDto yieldHistoryDto = (YieldHistoryDto) event.getResult();
                ApplicationBeanUtils.getBean(YieldPanel.class).setHistoryData(yieldHistoryDto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Event subscribe method for finishing grr task.
     *
     * @param event ExecutorTaskFinishEvent instance
     */
    @Subscribe
    public void grrOperateFinish(ExecutorTaskFinishEvent event) {
        try {
            if (event.getKey().equals(ProcessConstant.GRR_DECODE_FILE)) {
                GrrGroupDto dto = (GrrGroupDto) event.getResult();
                ApplicationBeanUtils.getBean(GrrMainPanel.class).loadGroup(dto);
            } else if (event.getKey().equals(ProcessConstant.GRR_SEARCH_SUB_TASK)) {
                GrrResultDto dto = (GrrResultDto) event.getResult();
                ApplicationBeanUtils.getBean(AnalysisPanel.class).loadSubItem(dto);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
